package com.wawayaya.parentteacher.ui;


import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.badlogic.gdx.utils.Array;
import com.wawayaya.base.BaseFragment;
import com.wawayaya.parentteacher.bean.BookShelf;
import com.wawayaya.parentteacher.bean.Books;
import com.wawayaya.parentteacher.widget.TpBookRack;
import com.wawayaya.util.event.ShelfEvent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import wawayaya.kids_iread.R;

/**
 * Created by wang_qian on 2015/12/7.
 */
public class SelectBookFragment extends BaseFragment {

    private JSONObject     selectJason;
    private LinearLayout   llcontent;
    private ArrayList<BookShelf> bookShelfList;
    private int             mLibClass;
    private boolean         mIsZh;

    public  static SelectBookFragment newInstance(String   selectjasonstr, int libclass) {
        SelectBookFragment fragement = new SelectBookFragment();

        Bundle bundle = new Bundle();
        bundle.putString("jasonstr", selectjasonstr);
        bundle.putInt("libclass", libclass);
        fragement.setArguments(bundle);


        return fragement;
    }


    @Override
    public void onEvent(ShelfEvent eventData) {

    }

    @Override
    public void clear() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       JSONArray  selectobj = null;
       Bundle   args = getArguments();
       String selectstr =  args.getString("jasonstr");
        mLibClass = args.getInt("libclass");
       if (!TextUtils.isEmpty(selectstr)) {
           try {
               selectobj = new JSONArray(selectstr);
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }

        loadData(selectobj);

        View view = inflater.inflate(R.layout.tp_selectfragment_layout, container, false);
        initViews(inflater, view);




        return view;
    }

    private void loadData(JSONArray  bookshelfArray) {

        // clear old data;
        if (bookShelfList != null && bookShelfList.size() > 0) {
            int bookshelfnum = bookShelfList.size();
            for (int i = 0; i < bookshelfnum; i++) {
                BookShelf bookshelf = bookShelfList.get(i);
                if (bookshelf != null && bookshelf.getBooksList() != null) {
                    List<Books> boolist = bookshelf.getBooksList();
                    if (boolist != null) boolist.clear();
                }
            }
            bookShelfList.clear();
        }

        //parse jason data to construct new data.
        if (bookshelfArray != null && bookshelfArray.length() > 0) {
            int bookshelfnum = bookshelfArray.length();
            for (int i = 0; i< bookshelfnum; i++) {
                JSONObject bookshelfja = bookshelfArray.optJSONObject(i);
                if (bookshelfja != null) {
                    if (bookShelfList == null) bookShelfList = new ArrayList<BookShelf>();

                    BookShelf bookshelf = new BookShelf();

                    if (bookshelfja.has("bs")) {
                        JSONObject jobookshelf = bookshelfja.optJSONObject("bs");
                        bookshelf.setBsId(jobookshelf.optLong("bs_id"));
                        bookshelf.setBsNameEn(jobookshelf.optString("bs_name"));
                        bookshelf.setBsNameZh(jobookshelf.optString("bs_name"));
                        bookshelf.setBsIconUri(jobookshelf.optString("bs_icon_uri"));
                    }

                    if (bookshelfja.has("books")) {
                        JSONArray bookarray = bookshelfja.optJSONArray("books");
                        if (bookarray != null && bookarray.length() > 0) {
                            ArrayList<Books> booklist = new ArrayList<Books>();
                            int book_num =  bookarray.length();
                            for (int j = 0; j < book_num; j++) {
                                JSONObject bookjo = bookarray.optJSONObject(j);
                                if (bookjo != null) {
                                    Books book = new Books();
                                    book.setBookId(bookjo.optLong("book_id"));
                                    book.setNameEn(bookjo.optString("book_name"));
                                    book.setNameZh(bookjo.optString("book_name"));
                                    book.setThumb(bookjo.optString("book_cover_uri"));
                                    booklist.add(book);
                                }
                            }
                            bookshelf.setBooksList(booklist);
                        }
                    }
                    bookShelfList.add(bookshelf);
                }
            }
        }
    }

    private void initViews(LayoutInflater inflater, View rootview) {
        llcontent = (LinearLayout)rootview.findViewById(R.id.ll_selectbook_content);
        llcontent.removeAllViews();


        if (bookShelfList != null && bookShelfList.size() > 0) {
            int bookshelf_num = bookShelfList.size();
            for (int i = 0; i < bookshelf_num; i++) {
                View bookrackbiew = inflater.inflate(R.layout.tp_bookrack_layout, null);
                TpBookRack bookrack = new TpBookRack(getContext(), bookrackbiew, bookShelfList.get(i), mIsZh, mLibClass);
                llcontent.addView(bookrack.getView());
            }
        }
    }
}
